const { list, tab, activeList } = require("./mock");
const bodyParser = require("body-parser");
const Mock = require("mockjs");
const userList = [
  {
    username: "123@qq.com",
    password: "123456",
    nickname: "小三三",
    avator: Mock.Random.image("30x30", Mock.Random.color()),
    id: Mock.Random.id(),
  },
  {
    username: "123@163.com",
    password: "666666",
    nickname: "空间",
    avator: Mock.Random.image("30x30", Mock.Random.color()),
    id: Mock.Random.id(),
  },
];
module.exports = (app) => {
  app.use(bodyParser.json());
  app.get("/api/list", (req, res) => {
    res.send({
      code: 200,
      data: list,
    });
  });

  app.post("/api/list/add", (req, res) => {
    const { title, typeList, content, nickname, avator } = req.body;
    list.push({
      title,
      city: "上海",
      nickname,
      avator,
      date: new Date().toLocaleString(),
      typeList,
      id: Mock.Random.id(),
    });
    res.send({
      code: 200,
      message: "添加成功！！！",
    });
  });

  app.get("/api/tab", (req, res) => {
    res.send({
      code: 200,
      data: tab,
    });
  });

  app.post('/api/login', (req, res) => {
    const { username, password } = req.body;
    const user = userList.find(v => v.username === username && v.password === password)
    if(user)   {
      res.send({
        code: 200,
        data: {
          nickname: user.nickname,
          avator: user.avator,
          token: Math.random().toString().slice(2)
        },
        message: '登录成功， 欢迎' + user.nickname
      })
    } else {
      res.send({
        code: 401,
        data: null,
        message: '账户密码错误， 请重新登录'
      })
    }
  })


  app.get('/api/active', (req, res) =>{
    res.send({
      code: 200,
      data: activeList
    })
  })
};
